﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;

public class DialogScreen : GameScreen
{
    private Texture2D character, marca, dark;
    private Texture2D textSpace, textSpace2, textSpace3;

    private Dictionary<int, Dialog> dialogos;
    private int actualDialog;

    private int select;

    private SpriteFont font;

    private NPC npc;
    private string name;

    private string move = "Audio/s02";
    private string accept = "Audio/s01";

    public DialogScreen(NPC npc)
    {
        IsPopup = true;
        Globals.IsPaused = true;

        actualDialog = 0;

        PlayerDialog[] pla;

        this.npc = npc;

        dialogos = new Dictionary<int, Dialog>();

        Dictionary<int, Dialog> aux = new Dictionary<int, Dialog>();

        aux = npc.Dialogos;

        foreach (int d in aux.Keys)
        {
            Dialog dialog = new Dialog();

            foreach (NPCDialog npcd in aux[d].NPCDialog)
            {
                npcd.Text = npcd.Text.Replace("$jogador", GameSession.Player.Name);
                dialog.NPCDialog.Add(npcd);
            }

            foreach (PlayerDialog playerd in aux[d].PlayerDialog)
            {
                if (GameSession.CheckAllConditions(playerd.Condition))
                {
                    playerd.Text = playerd.Text.Replace("$jogador", GameSession.Player.Name);
                    dialog.PlayerDialog.Add(playerd);
                }
            }

            dialogos.Add(d, dialog);
        }

        character = npc.Face;

        name = npc.Name;

        for (int i = npc.Start.Count - 1; i >= 0; i--)
        {
            if (dialogos[npc.Start[i]].NPCDialog.Count == 0)
                continue;

            if (GameSession.CheckAllConditions(dialogos[npc.Start[i]].NPCDialog[0].Need))
            {
                actualDialog = npc.Start[i];
                break;
            }
        }
    }

    public DialogScreen()
    {
        IsPopup = true;
        Globals.IsPaused = true;

        actualDialog = 0;

        PlayerDialog[] pla;

        npc = GameSession.GetNPC(Day.ONE, 0);

        dialogos = new Dictionary<int,Dialog>();

        Dictionary<int, Dialog> aux = new Dictionary<int,Dialog>();

        aux = GameSession.GetDialog(Day.ONE, 0);

        foreach (int d in aux.Keys)
        {
            Dialog dialog = new Dialog();
            
            foreach (NPCDialog npcd in aux[d].NPCDialog)
            {
                dialog.NPCDialog.Add(npcd);
                /*if (GameSession.CheckCondition(npcd.Need))
                {
                    if (npcd.Atributo == Atributo.NONE)
                        dialog.NPCDialog.Add(npcd);
                    if (npcd.Atributo == Atributo.CARISMA && GameSession.GetCarisma() >= npcd.Quantidade)
                        dialog.NPCDialog.Add(npcd);
                    if (npcd.Atributo == Atributo.PERCEPCAO && GameSession.GetPercepcao() >= npcd.Quantidade)
                        dialog.NPCDialog.Add(npcd);
                    if (npcd.Atributo == Atributo.INTELIGENCIA && GameSession.GetInteractionCount() >= npcd.Quantidade)
                        dialog.NPCDialog.Add(npcd);
                }*/
            }

            foreach (PlayerDialog playerd in aux[d].PlayerDialog)
            {
                if (GameSession.CheckAllConditions(playerd.Condition))
                {
                    dialog.PlayerDialog.Add(playerd);
                }
            }

            dialogos.Add(d, dialog);
        }

        character = npc.Face;

        name = npc.Name;
        
        for (int i = npc.Start.Count - 1; i >= 0; i--)
        {
            if (dialogos[npc.Start[i]].NPCDialog.Count == 0)
                continue;

            if (GameSession.CheckAllConditions(dialogos[npc.Start[i]].NPCDialog[0].Need))
            {
                actualDialog = npc.Start[i];
                break;
            }
        }

    }

    public override void LoadContent()
    {
        base.LoadContent();

        dark = GameSession.Content.Load<Texture2D>("Textures/dark");
        textSpace2 = GameSession.Content.Load<Texture2D>("Textures/paper2");//("Dialogo/baixo");
        textSpace = GameSession.Content.Load<Texture2D>("Textures/paper");
        textSpace3 = GameSession.Content.Load<Texture2D>("Textures/paper3");
        marca = GameSession.Content.Load<Texture2D>("Textures/marca");

        font = GameSession.Content.Load<SpriteFont>("SpriteFont1");
    }

    public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
    {
        base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

        if (Globals.FirstDialog)
        {
            ScreenManager.AddScreen(new TutorialScreen(TutorialScreen.TutorialKind.DIALOG));
            Globals.FirstDialog = false;
        }
    }

    public override void Draw(GameTime gameTime)
    {
        base.Draw(gameTime);

        SpriteBatch spriteBatch = ScreenManager.SpriteBatch;

        spriteBatch.Begin();

        spriteBatch.Draw(dark, Vector2.Zero, Color.White);

        if (character != null)
        {
            spriteBatch.Draw(character, new Rectangle(Globals.ScreenWidth / 2 - character.Width / 2, 120, character.Width, character.Height), Color.White);
            spriteBatch.Draw(textSpace2, new Rectangle(Globals.ScreenWidth / 2 - textSpace2.Width / 2, 20, textSpace2.Width, textSpace2.Height), Color.White);
        }
        else
        {
            spriteBatch.Draw(textSpace3, new Rectangle(Globals.ScreenWidth / 2 - textSpace3.Width / 2, 20, textSpace3.Width, textSpace3.Height), Color.White);
            spriteBatch.Draw(npc.Front, new Vector2(Globals.ScreenWidth / 2 - textSpace2.Width / 2, 20), Color.White);
        }
        spriteBatch.Draw(textSpace, new Rectangle(Globals.ScreenWidth / 2 - textSpace.Width / 2, 600, textSpace.Width, textSpace.Height), Color.White); //burlywood

        //spriteBatch.DrawString(font, name, new Vector2(400, 120), Color.Black);

        WriteString(spriteBatch, dialogos[actualDialog].NPCText, 30, false);

        int x = 250, y = 610;

        int count = 0;
        int size = 0;

        foreach (PlayerDialog player in dialogos[actualDialog].PlayerDialog)
        {
            bool selected = false;
            if(count == select)
                selected = true;

            int aux = WriteString(spriteBatch, player.Text, y, selected);
            count ++;
            y += 25 * aux;

            if (count <= select)
                size+=aux;
        }

        spriteBatch.Draw(marca, new Vector2(245 - marca.Width, 630 + size * 25 - marca.Height), Color.White);

        spriteBatch.End();
    }

    private int WriteString(SpriteBatch spriteBatch, string text, int y, bool selected)
    {
        string[] words = text.Split(' ');

        string aux = "";

        int draws = 0;

        for (int i = 0; i < words.Length; i++ )
        {
            if(aux.Equals(""))
                aux += words[i];
            else
                aux += " " + words[i];

            //if (aux.Length > 80)
            if(font.MeasureString(aux).X > 750)
            {
                if (selected)
                    spriteBatch.DrawString(font,aux, new Vector2(250, y + draws * 25), Color.Red);
                else
                    spriteBatch.DrawString(font, aux, new Vector2(250, y + draws * 25), Color.Black);
                draws++;
                aux = "";
            }
        }
        if (selected)
            spriteBatch.DrawString(font, aux, new Vector2(250, y + draws * 25), Color.Red);
        else
            spriteBatch.DrawString(font, aux, new Vector2(250, y + draws * 25), Color.Black);

        draws++;

        return draws;
    }

    public override void HandleInput()
    {
        if (InputManager.IsActionTriggered(InputManager.Action.Back))
        {
            Exit();
        }

        if (InputManager.IsActionTriggered(InputManager.Action.MovePlayer1Down))
        {
            int oldselect = select;

            select++;

            if (select >= dialogos[actualDialog].PlayerDialog.Count)
                select = 0;

            if (oldselect != select)
                AudioManager.PlaySound(move);
        }

        if (InputManager.IsActionTriggered(InputManager.Action.MovePlayer1Up))
        {
            int oldselect = select;

            select--;
            if (select < 0)
                select = dialogos[actualDialog].PlayerDialog.Count - 1;

            if (oldselect != select)
                AudioManager.PlaySound(move);
        }

        if (InputManager.IsActionTriggered(InputManager.Action.Space))
        {
            AudioManager.PlaySound(accept);

            dialogos[actualDialog].PlayerDialog[select].GiveCondition();

            if (dialogos[actualDialog].PlayerDialog[select].Next.Count == 0)
            {
                dialogos[actualDialog].NPCDialog[0].GiveCondition();
                Exit();
                return;
            }

            for (int i = dialogos[actualDialog].PlayerDialog[select].Next.Count - 1; i >= 0; i--)
            {
                NPCDialog n = dialogos[dialogos[actualDialog].PlayerDialog[select].Next[i]].NPCDialog[0];

                if (GameSession.CheckAllConditions(n.Need))
                {
                    actualDialog = dialogos[actualDialog].PlayerDialog[select].Next[i];
                    break;
                }
            }

            //actualDialog = dialogos[actualDialog].PlayerDialog[select].Next;
            select = 0;
            if (!dialogos.ContainsKey(actualDialog))
            {
                dialogos[actualDialog].NPCDialog[0].GiveCondition();
                Exit();
            }

        }
    }

    private void Exit()
    {
        Globals.IsPaused = false;
        this.ExitScreen();
    }
}
